package com.kedacom.freedomLabel.viewmodel.item;


import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.base.BaseViewModel;
import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.db.Converters;
import com.kedacom.freedomLabel.utils.Cn2Spell;
import com.kedacom.lego.message.LegoMessageManager;
import com.kedacom.lego.mvvm.ObservableLiveDataField;
import com.kedacom.lego.mvvm.bindadapter.RadioGroupViewAdapter.RadioCheckState;
import com.kedacom.lego.mvvm.command.BindingCommand;
import com.kedacom.lego.mvvm.command.BindingConsumer;
import com.lzscale.scalelib.misclib.Misc;

public class IDControlViewModel extends BaseViewModel {

    public ObservableLiveDataField<String> evNameText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evNumberText = new ObservableLiveDataField<>();
    public ObservableLiveDataField<String> evRemarkText = new ObservableLiveDataField<>();
    private Misc mMisc = Misc.newInstance();
    private String mItemCategory = Converters.sDefaultColumnNames[0];

    public BindingCommand radioGroupOnCheckedChanged = BindingCommand.build((BindingConsumer<RadioCheckState>) input -> {
        if (input.isChecked) {
            mMisc.beep();
            int index = input.index;
            if (index > -1 && index < Converters.sDefaultColumnNames.length) {
                String category = Converters.sDefaultColumnNames[index];
                mItemCategory = category;
                String[] params = new String[]{category, null};
                LegoMessageManager.getInstance().sendMessage(Options.MSG_UPDATE_LIST_PARAM, params);
                LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_RECOVERY_ITEM_DETAIL);
            }
        }
    });

    public void saveID() {
        String name = evNameText.getValue();
        String number = evNumberText.getValue();
        String remark = evRemarkText.getValue();

        Item item = new Item();
        item.category = mItemCategory;
        item.value = name;
        item.shortHand = Cn2Spell.getPinYinHeadChar(item.value);
        item.number = number;
        item.remark = remark;
        item.createTime = System.currentTimeMillis();

        LegoMessageManager.getInstance().sendMessage(Options.MSG_ADD_ITEM, item);
    }

    public void updateID(Item item) {
        String name = evNameText.getValue();
        String number = evNumberText.getValue();
        String remark = evRemarkText.getValue();

        item.value = name;
        item.shortHand = Cn2Spell.getPinYinHeadChar(item.value);
        item.number = number;
        item.remark = remark;
        item.updateTime = System.currentTimeMillis();

        LegoMessageManager.getInstance().sendMessage(Options.MSG_SAVE_ITEM, item);
    }
}